static int tolua_TypeCast(lua_State* tolua_S)
{
  CCObject* obj = (CCObject*)  tolua_tousertype(tolua_S,1,0);
  const char* type = ((const char*)  tolua_tostring(tolua_S,2,0));
  {
    //CCLog("%p %s", obj, type);
    CCObject* tolua_ret = obj;
    int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1;
    int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL;
    toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)obj,type);
  }
  return 1;
}

static int tolua_usecond(lua_State* tolua_S){
  timeval t;
  gettimeofday(&t, NULL);
  double tolua_ret = t.tv_sec + t.tv_usec/1000000.0;
  //printf("tolua_usecond %d %d %f\n", t.tv_sec, t.tv_usec, tolua_ret);
  tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
  return 1;
}

static int tolua_setObjectExtTable(lua_State* tolua_S)
{
  CCObject* obj = (CCObject*)  tolua_tousertype(tolua_S,1,0);
  //const char* type = ((const char*)  tolua_tostring(tolua_S,2,0));
  int ret = lua_istable(tolua_S, -1);
  //printf("### tolua_setObjectExtTable %d %p\n", ret, obj);
  lua_getglobal(tolua_S, "ObjectExtTable");
  char key[64];
  sprintf(key, "userdata: %p", obj);
  lua_pushstring(tolua_S, key);
  lua_pushvalue(tolua_S, -3);
  lua_settable(tolua_S, -3);
  lua_pop(tolua_S, 1);
  return 1;
}

static int tolua_addMenuItemHandler(lua_State* tolua_S)
{
  CCMenuItem* menuItem = (CCMenuItem*)tolua_tousertype(tolua_S,1,0);
  const char* callback = ((const char*)  tolua_tostring(tolua_S,2,0));
  menuItem->setTarget(ButtonHandler::sharedButtonHandler(), (SEL_MenuHandler)&ButtonHandler::menuHandle);
  ButtonHandler::sharedButtonHandler()->addName(callback, menuItem);
  return 1;
}


void setTouchEnabledAll(CCNode* node, bool enable){
  //printf("node %s %d\n", typeid(*node).name(), enable);
  CCControl* cl = dynamic_cast<CCControl*>(node);
  if(cl){
    cl->setEnabled(enable);
  }else{
    CCLayer* l = dynamic_cast<CCLayer*>(node);
    if(l){
      l->setTouchEnabled(enable);
    }
  }
  int c = node->getChildrenCount();
  if(c==0){
    return;
  }
  CCArray* children = node->getChildren();
  for(int i=0; i<children->count(); i++){
    CCNode* child = (CCLayer*)children->objectAtIndex(i);
    setTouchEnabledAll(child, enable); 
  } 
}

static int tolua_setTouchEnabledAll(lua_State* tolua_S)
{
  CCNode* layer = (CCNode*)  tolua_tousertype(tolua_S,1,0);
  //int enable = ((int)  tolua_tonumber(tolua_S,2,0));
  bool enable= ((bool)  tolua_toboolean(tolua_S,2,true));
  //printf("tolua_setTouchEnabledAll %d\n", enable);
  {
    setTouchEnabledAll(layer, enable);
  }
  return 1;
}
